package com.eascs.archive.dao;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.archive.entity.CreditArchiveRecordEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ArchiveCreditDao extends BaseO2oDao {
	private final static String Credit_Finish_Order_SQL = "SELECT t.bill_code, t.loanapply_no, u.loan_id, u.user_full_name, "
			+ " u.document_no, y.datalist_code, y.creat_date, y.datalist_name "
			+ " FROM o2o_loanapply t LEFT JOIN o2o_datalist y ON t.loanapply_no = y.loanapply_no "
			+ " AND y.is_delete = 'N' LEFT JOIN o2o_loan_user u ON u.loanapply_no = t.loanapply_no ";

	public boolean saveCreditArchive(CreditArchiveRecordEntity entity) {
		return insert(entity) > 0;
	}

	/**
	 * 查找单据的状态还处在征信完成的单据
	 * 
	 * @return
	 */
	public List<Object[]> getCreditFinishOrder() {
		String sql = Credit_Finish_Order_SQL
				+ " AND t.loan_id = u.loan_id AND u.spouse_id IS NULL AND u.sponsor_type IS NULL "
				+ " WHERE  y.datalist_code = 'ZXBGDATA'";
		return findObjectsList(sql);
	}

	/**
	 * 查找单据的状态还处在征信完成的单据(配偶)
	 * 
	 * @return
	 */
	public List<Object[]> getCreditFinishOrderForSpouse() {
		String sql = Credit_Finish_Order_SQL + " AND t.loan_id = u.spouse_id AND u.sponsor_type IS NULL "
				+ " WHERE  y.datalist_code = 'POZXBGDATA'";
		return findObjectsList(sql);
	}

	/**
	 * 查找单据的状态还处在征信完成的单据(担保人)
	 * 
	 * @return
	 */
	public List<Object[]> getCreditFinishOrderForSurety() {
		String sql = Credit_Finish_Order_SQL + " AND u.sponsor_type = 'D' " + " WHERE y.datalist_code = 'SPONSOR_ZXBG'";
		return findObjectsList(sql);
	}

	/**
	 * 根据申请单号，贷款单号和客户ID查询归档记录
	 * 
	 * @param loanapplyNo
	 * @param billCode
	 * @param customerId
	 * @return
	 */
	public CreditArchiveRecordEntity queryCreditArchiveRecord(String loanapplyNo, String billCode, String customerId) {
		String sql = "SELECT * FROM o2o_credit_archive_record t WHERE t.loanapply_no=? AND t.bill_code=? AND t.customer_id=?";
		return findEntity(sql, CreditArchiveRecordEntity.class, loanapplyNo, billCode, customerId);
	}

	/**
	 * 分页查询征信归档数据
	 */
	public PageResult<Object[]> pageCreditArchiveRecord() {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 100, 1);

		StringBuffer querySql = new StringBuffer("SELECT t.province, t.platform_code, t.loanee,"
				+ " t.loan_amount, t.allocated_time, t.submit_report_time"
				+ ", t.declare_status, t.remark, t.follower FROM bank_loan_progress_follow t WHERE 1=1 ");
		/*
		 * if (StringUtil.isNotBlank(form.getAllocatedTimeStart())) {
		 * querySql.append(" AND allocated_time>=?");
		 * sqlPageQuery.addParam(form.getAllocatedTimeStart()); } if
		 * (StringUtil.isNotBlank(form.getAllocatedTimeEnd())) {
		 * querySql.append(" AND allocated_time<=?");
		 * sqlPageQuery.addParam(form.getAllocatedTimeEnd()); } if
		 * (StringUtil.isNotBlank(form.getDeclareStatus())) { querySql.append(
		 * " AND declare_status=?");
		 * sqlPageQuery.addParam(form.getDeclareStatus()); }
		 */
		sqlPageQuery.setQuerySql(querySql.toString());

		return findDataWithPage(sqlPageQuery);
	}

	public PageResult<Object[]> queryArchiveCreditInfo(SqlPageQuery<Object[]> pageQuery) {
		return this.findDataWithPage(pageQuery);
	}

	/**
	 * 新征信归档查询
	 * 
	 * @return
	 */
	public List<Object[]> getNewCreditFinishOrder() {
		String sql = "SELECT t.id,t.bill_code,t1.loan_id,t1.user_full_name,t1.document_no,t.zx_type,t1.zx_date,t.ltd from o2o_zx t,o2o_loan_user_zx t1 "
				+ " where t.id = t1.zx_no and t.zx_status ='CLO' ";
		return this.findObjectsList(sql);
	}

}
